package Z07JDBC.Z08JDBCpool;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author 李某
 * * @date 2021/10/21
 * *物有本末，事有终始。知其先后者，则近道矣！
 * *荷花开满池塘的前一天还是半塘荷花半塘云。
 */
public class TestTestPool {
    public static void main(String[] args) {
        ConnectionPool cp = new ConnectionPool(3);
        for (int i = 0;i < 100 ;i++){
            new WorkingThread("working thread " + i,cp).start();
        }
    }
}

class WorkingThread extends Thread{
    private  ConnectionPool cp;
    public WorkingThread(String name,ConnectionPool cp){
        super(name);
        this.cp=cp;
    }

    public void run(){
        Connection c = cp.getConnection();
        System.out.println(this.getName() + ":\t 获取连接并且开始工作");
        try(Statement st = c.createStatement()) {
            Thread.sleep(1000);

            try {
                st.execute("select * from hero");
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (SQLException | InterruptedException e) {
            e.printStackTrace();
        }
        cp.returnConnection(c);
    }
}